{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a5a3a1a7-97ab-4b7e-970c-a62b8fe1b45e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import wget\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ebb01d66-a750-4f1b-aba8-65f5ea3e6461",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading: ATP\n",
      "100% [............................................................................] 258824 / 258824Downloading: ADP\n",
      "100% [............................................................................] 309267 / 309267Downloading: GTP\n",
      "100% [..............................................................................] 82648 / 82648Downloading: GDP\n",
      "100% [..............................................................................] 84786 / 84786Downloading: NAD\n",
      "100% [............................................................................] 108965 / 108965Downloading: FAD\n",
      "100% [............................................................................] 150034 / 150034Downloading: FMN\n",
      "100% [..............................................................................] 69101 / 69101Downloading: UDP\n",
      "100% [..............................................................................] 51890 / 51890"
     ]
    }
   ],
   "source": [
    "## Download the data\n",
    "DSET_URL = {\n",
    "    \"ATP\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/ATP.txt\",\n",
    "    \"ADP\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/ADP.txt\",\n",
    "    \"GTP\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/GTP.txt\",\n",
    "    \"GDP\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/GDP.txt\",\n",
    "    \"NAD\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/NAD.txt\",\n",
    "    \"FAD\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/FAD.txt\",\n",
    "    \"FMN\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/FMN.txt\",\n",
    "    \"UDP\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/UDP.txt\",\n",
    "}\n",
    "\n",
    "for k, v in DSET_URL.items():\n",
    "    print(f\"Downloading: {k}\") \n",
    "    wget.download(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b408f983-e8ca-44a8-b90d-3877696700ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "\n",
    "def parse_dset(filename: str, write: bool = False) -> pd.DataFrame:\n",
    "    with open(filename) as fasta_file:  # Will close handle cleanly\n",
    "        identifiers = []\n",
    "        chains = []\n",
    "        sequences = []\n",
    "        interactions = []\n",
    "        lengths = []\n",
    "        for seq_record in SeqIO.parse(fasta_file, 'fasta'):  # (generator)\n",
    "            identifiers.append(seq_record.id[:-1])\n",
    "            chains.append(seq_record.id[-1])\n",
    "            lengths.append(len(seq_record.seq)/2)\n",
    "\n",
    "            parsed_sequence_and_interactions = re.split(';', re.sub(\"\\+|-\", lambda match: ';' + match.group(), str(seq_record.seq), count=1), maxsplit=1)#str(seq_record.seq).split(\"-\", maxsplit=1)\n",
    "            sequences.append(parsed_sequence_and_interactions[0])\n",
    "            interactions.append(parsed_sequence_and_interactions[1])\n",
    "\n",
    "    data = zip(identifiers, chains, sequences, interactions,lengths)\n",
    "    df = pd.DataFrame.from_dict(data)\n",
    "    df.columns = [\"PDB\", \"chain\", \"sequence\", \"interacting_residues\", \"length\"]\n",
    "    df[\"interactor\"] = filename.split(\".\")[0]\n",
    "    \n",
    "    if write:\n",
    "        df.to_csv(filename.split(\".\")[0] + \".csv\")\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "01c9b42f-70e3-496c-a7f4-5cefbb86376e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "## Iterate over datasets\n",
    "dataset_list = [parse_dset(k + \".txt\", write=True) for k in DSET_URL.keys()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "23f8b6c4-fb8c-4090-94a1-f4880b1db6cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.concat(dataset_list)\n",
    "df.reset_index(inplace=True, drop=True)\n",
    "df.to_csv(\"PROTEINS_NUCLEOTIDES.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f465b896-8340-45a0-b515-1596f9790b26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDB</th>\n",
       "      <th>chain</th>\n",
       "      <th>sequence</th>\n",
       "      <th>interacting_residues</th>\n",
       "      <th>length</th>\n",
       "      <th>interactor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1a0i</td>\n",
       "      <td>A</td>\n",
       "      <td>VNIKTNPFKAVSFVESAIKKALDNAGYLIAEIKYDGVRGNICVDNT...</td>\n",
       "      <td>------------------------------+-++---+--------...</td>\n",
       "      <td>332.0</td>\n",
       "      <td>ATP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1a49</td>\n",
       "      <td>A</td>\n",
       "      <td>IQTQQLHAAMADTFLEHMCRLDIDSAPITARNTGIICTIGPASRSV...</td>\n",
       "      <td>-------------------------------------+--+-----...</td>\n",
       "      <td>519.0</td>\n",
       "      <td>ATP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1a82</td>\n",
       "      <td>A</td>\n",
       "      <td>SKRYFVTGTDTEVGKTVASCALLQAAKAAGYRTAGYKPVASGSEKT...</td>\n",
       "      <td>----------+++++++-------------------+---------...</td>\n",
       "      <td>224.0</td>\n",
       "      <td>ATP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1asz</td>\n",
       "      <td>A</td>\n",
       "      <td>EDTAKDNYGKLPLIQSRDSDRTGQKRVKFVDLDEAKDSDKEVLFRA...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>490.0</td>\n",
       "      <td>ATP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1b76</td>\n",
       "      <td>A</td>\n",
       "      <td>AASSLDELVALCKRRGFIFQSSEIYGGLQGVYDYGPLGVELKNNLK...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>442.0</td>\n",
       "      <td>ATP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1361</th>\n",
       "      <td>5huu</td>\n",
       "      <td>A</td>\n",
       "      <td>GKVLVVSNRIPVTIKRLDNGSYDYSMSSGGLVTALQGLKKTTEFQW...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>469.0</td>\n",
       "      <td>UDP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1362</th>\n",
       "      <td>5lvv</td>\n",
       "      <td>A</td>\n",
       "      <td>HHVPYSSAQSTSKTSVTLSLGGGTHADSLNNLANIKREQGNIEEAV...</td>\n",
       "      <td>--++++----------------------------------------...</td>\n",
       "      <td>720.0</td>\n",
       "      <td>UDP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1363</th>\n",
       "      <td>5n80</td>\n",
       "      <td>A</td>\n",
       "      <td>AMKIAFIGEAVSGFGGMETVISNVIHTFENSSPKINCEMFFFCRND...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>358.0</td>\n",
       "      <td>UDP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1364</th>\n",
       "      <td>5ndf</td>\n",
       "      <td>A</td>\n",
       "      <td>KVRWPDFNQEAYVGGTMVRSGQDPYARNKFNQVESDKLRMDRAIPD...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>495.0</td>\n",
       "      <td>UDP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1365</th>\n",
       "      <td>6eyt</td>\n",
       "      <td>A</td>\n",
       "      <td>SGSANFAGVEYPLLPLDQHTPLLFQWFERNPSRFGENQIPIINTQQ...</td>\n",
       "      <td>------------------------+++-+-----------------...</td>\n",
       "      <td>309.0</td>\n",
       "      <td>UDP</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1366 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       PDB chain                                           sequence  \\\n",
       "0     1a0i     A  VNIKTNPFKAVSFVESAIKKALDNAGYLIAEIKYDGVRGNICVDNT...   \n",
       "1     1a49     A  IQTQQLHAAMADTFLEHMCRLDIDSAPITARNTGIICTIGPASRSV...   \n",
       "2     1a82     A  SKRYFVTGTDTEVGKTVASCALLQAAKAAGYRTAGYKPVASGSEKT...   \n",
       "3     1asz     A  EDTAKDNYGKLPLIQSRDSDRTGQKRVKFVDLDEAKDSDKEVLFRA...   \n",
       "4     1b76     A  AASSLDELVALCKRRGFIFQSSEIYGGLQGVYDYGPLGVELKNNLK...   \n",
       "...    ...   ...                                                ...   \n",
       "1361  5huu     A  GKVLVVSNRIPVTIKRLDNGSYDYSMSSGGLVTALQGLKKTTEFQW...   \n",
       "1362  5lvv     A  HHVPYSSAQSTSKTSVTLSLGGGTHADSLNNLANIKREQGNIEEAV...   \n",
       "1363  5n80     A  AMKIAFIGEAVSGFGGMETVISNVIHTFENSSPKINCEMFFFCRND...   \n",
       "1364  5ndf     A  KVRWPDFNQEAYVGGTMVRSGQDPYARNKFNQVESDKLRMDRAIPD...   \n",
       "1365  6eyt     A  SGSANFAGVEYPLLPLDQHTPLLFQWFERNPSRFGENQIPIINTQQ...   \n",
       "\n",
       "                                   interacting_residues  length interactor  \n",
       "0     ------------------------------+-++---+--------...   332.0        ATP  \n",
       "1     -------------------------------------+--+-----...   519.0        ATP  \n",
       "2     ----------+++++++-------------------+---------...   224.0        ATP  \n",
       "3     ----------------------------------------------...   490.0        ATP  \n",
       "4     ----------------------------------------------...   442.0        ATP  \n",
       "...                                                 ...     ...        ...  \n",
       "1361  ----------------------------------------------...   469.0        UDP  \n",
       "1362  --++++----------------------------------------...   720.0        UDP  \n",
       "1363  ----------------------------------------------...   358.0        UDP  \n",
       "1364  ----------------------------------------------...   495.0        UDP  \n",
       "1365  ------------------------+++-+-----------------...   309.0        UDP  \n",
       "\n",
       "[1366 rows x 6 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
